'''
顺时针打印矩阵
'''
####神仙操作
# array = [[1,2,3],[4,5,6],[7,8,9]]
# def spiralOrder(matrix):
#     res = []
#     while matrix:
#         res += matrix.pop(0)
#         matrix = list(zip(*matrix))[::-1]
#     return res
# print(spiralOrder(array))

###递归的打印最外层的值
# array = [[1,2,3],[4,5,6],[7,8,9]]
# res = []
# row = len(array)
# col = len(array[0])
# clocation = 0
# rlocation = 0
# while len(res)!=row*col:
#     for index,value in enumerate(array):
#         if index == 0:
#             res+=array[index]
#             clocation = index
#         elif index < len(array)-1:
#             res.append(array[index][len(array[index])-1])
#         else:
#             res+=array[index][::-1]
#             clocation = index
#     while clocation>1 and len(array[clocation])>=2:
#         res.append(array[clocation-1][0])
#         clocation-=1
#     rlocation+=1
#     midllearr = []
#     while clocation<len(array)-1:
#         midllearr.append(array[clocation][rlocation:len(array[index])-1])
#         clocation+=1
#     array = midllearr
#     rlocation -= 1
#
# print(res)

#定义四个方向下左右
matrix = [[1,2,3],[4,5,6],[7,8,9]]
top,bottom,left,right,order = 0,len(matrix)-1,0,len(matrix[0])-1,[]
while left <= right and top <= bottom:
    for column in range(left, right + 1):
        order.append(matrix[top][column])
    for row in range(top + 1, bottom + 1):
        order.append(matrix[row][right])
    if left < right and top < bottom:
        for column in range(right - 1, left, -1):
            order.append(matrix[bottom][column])
        for row in range(bottom, top, -1):
            order.append(matrix[row][left])
    left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1
print(order)







